﻿using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Browser;

namespace EasySL.Controls
{
    public class ExDragItem : ContentControl
    {
        public ExDataGrid OwnerExDataGrid { get; set; }
        public double MinDistance { get; set; }
        public object ItemContent { get; set; }
        
        public ExDragItem()
            : base()
        {
            this.DefaultStyleKey = typeof(ExDragItem);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.content = this.GetTemplateChild("content") as ContentPresenter;
            if (this.Content == null || !(this.Content is ExDataGridColumn))
            {
                this.content.Content = ItemContent;
                return;
            }
                     
            string bindProperty = (this.Content as ExDataGridColumn).BindProperty;
            this.content.Content = bindProperty;

            var ghostItem = new ExDragItem();
            ghostItem.Opacity = 0.6;
            ghostItem.ItemContent = bindProperty;

            ExDropLocation nearestDropLocation = new ExDropLocation();
            
            MouseEventHandler onMove = (s, e) =>
            {
                if (this.OwnerExDataGrid == null)
                    return;
              
                double nearestDistance = 1000000;
                Point point;
                double pointDistance = 0;

                foreach (var location in OwnerExDataGrid.DropLocations)
                {
                    point = e.GetPosition(location);
                    pointDistance = Math.Sqrt(Math.Pow(point.X, 2) + Math.Pow(point.Y, 2));

                    if (pointDistance < nearestDistance)
                    {
                        nearestDistance = pointDistance;
                        nearestDropLocation = location;
                    }
                }

                OwnerExDataGrid.DropLocations.ForEach(dl => dl.Hide());
                nearestDropLocation.Show();
            };


            MouseButtonEventHandler onDrop = (s,e)=>
            {
                nearestDropLocation.Hide();
                
                if (this.OwnerExDataGrid == null)
                    return;

                foreach (ExDataGridColumn c in this.OwnerExDataGrid.Columns)
                {
                    if (c.BindProperty == bindProperty)
                    {
                        HtmlPage.Window.Alert("This column has existed in the datagrid");
                        return;
                    }
                }

                string locationProperty = nearestDropLocation.BindProperty;
                if (string.IsNullOrEmpty(locationProperty))
                    return;

                var column = this.Content as ExDataGridColumn;
                if (column != null)
                {
                    foreach (ExDataGridColumn c in this.OwnerExDataGrid.Columns)
                    {
                        if (c.BindProperty == locationProperty)
                        {
                            int index = c.DisplayIndex + 1;
                            if (index >= this.OwnerExDataGrid.Columns.Count)
                                index = this.OwnerExDataGrid.Columns.Count;

                            this.OwnerExDataGrid.Columns.Add(column);
                            column.DisplayIndex = index;
                            break;
                        }
                    }
                }
            };

            this.Drag(ghostItem, onMove, onDrop);
        }

        private ContentPresenter content;
    }
}
